package com.mica.erp.contract.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.mica.erp.contract.model.Contract;
import com.mica.erp.contract.model.Customer;
import com.mica.erp.contract.service.ContractService;
import com.mica.erp.contract.service.CustomerService;
import com.mica.mybatis.GenericService;

public class ContractServiceImpl extends GenericService implements ContractService {
	
	@Override
	public List<Contract> findContracts() {
		return this.selectList(namespace+".findContracts", null);
	}
	
	@Override
	public List<Contract> findContracts(Contract contract) {
		return this.selectList(namespace+".findContracts", contract);
	}

	@Override
	public Contract findContract(Contract contract) {
		return this.selectOne(namespace+".findContract", contract);
	}

	@Override
	public void saveContract(Contract contract) {
		Long cttId = contract.getCttId();
		if(null!=cttId){
			this.update(namespace+".updateContract", contract);
		}else{
			contract.setCttId(this.incre.nextLongValue());
			Long ctId = contract.getCttAId();
			if(null==ctId){
				Customer customer = new Customer();
				customer.setCtName(contract.getCctCustomer());
				customer.setCtBank(contract.getCctCBank());
				customer.setCtAccounts(contract.getCctCAccount());
				customer.setCtPost(contract.getCctCPost());
				customerService.saveCustomer(customer);
			}
			this.insert(namespace+".insertContract", contract);
		}
	}

	@Override
	public void delContract(Long cttId) {
		this.delete(namespace+".delContract", cttId);
	}
	
	@Autowired
	private CustomerService customerService;

	@Override
	public void updateContractBypayType(Contract contract) {
		Double amount = contract.getAmount();
		Double paidOn = contract.getPaidOn();
		if(amount.equals(paidOn)){
			contract.setPayType(1);
		}else{
			contract.setPayType(0);
		}
		this.update(namespace+".updateContractBypayType", contract);
	}

	@Override
	public void updateContractBysendType(Contract contract) {
		this.update(namespace+".updateContractBysendType", contract);
	}

	@Override
	public void updateContractByinvType(Contract contract) {
		this.update(namespace+".updateContractByinvType", contract);
	}

	@Override
	public void updateContractBycustomerType(Contract contract) {
		this.update(namespace+".updateContractBycustomerType", contract);
	}
}
